/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DebugDatabase.java
 *
 * Created on 27/Dez/2011, 22:31:02
 */

package intopo.evogame.partnerselection.tools.analyser;

import intopo.game.ReadStrategy;
import intopo.game.impl.givetake.GiveTakeStrategy;
import intopo.game.impl.givetake.ReadGiveTakeDeterministicStrategy;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;

/**
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 27/Dez/2011
 */
public class DebugDatabase
{
	static public void main (String[] args)
	{
		DataInputStream chromosome = null;
		ReadStrategy<GiveTakeStrategy> readStrategy;
		readStrategy = new ReadGiveTakeDeterministicStrategy ();
		ChromosomeRecord cr = new ChromosomeRecord<GiveTakeStrategy> (readStrategy);
		try {
			chromosome = new DataInputStream (new BufferedInputStream (new FileInputStream ("chromosome.tab"), 4096));
			Scanner s = new Scanner (System.in);
			boolean ctn = true;
			System.out.println ("Press enter to print next record, enter 'end' to stop");
			String line;
			while (ctn) {
				cr.read (chromosome, readStrategy);
				System.out.println (cr);
				ctn = !s.nextLine ().trim ().equals ("end");
			}
			ctn = true;
			RandomAccessFile raf = new RandomAccessFile ("chromosome.tab", "r");
			System.out.println ("Enter an integer, enter 'end' to stop");
			while (ctn) {
				line = s.nextLine ();
				try {
					int id = Integer.parseInt (line);
					cr.read (raf, id, readStrategy);
					System.out.println (cr);
				}
				catch (NumberFormatException e) {
					ctn = !line.trim ().equals ("end");
				}
			}
		}
		catch (FileNotFoundException ex) {
		}
		catch (IOException ex) {
			ex.printStackTrace ();
		}
		finally {
			if (chromosome != null) {
				try {
					chromosome.close ();
				}
				catch (IOException ex) {
				}
			}
		}
		debugPlayerFile ();
	}

	private static void debugPlayerFile ()
	{
		DataInputStream player = null;
		PlayerRecord pr = new PlayerRecord ();
		try {
			player = new DataInputStream (new BufferedInputStream (new FileInputStream ("player.tab"), 4096));
			Scanner s = new Scanner (System.in);
			boolean ctn = true;
			System.out.println ("Press enter to print next record, enter 'end' to stop");
			String line;
			while (ctn) {
				pr.read (player);
				System.out.println (pr);
				ctn = !s.nextLine ().trim ().equals ("end");
			}
			ctn = true;
			RandomAccessFile raf = new RandomAccessFile ("player.tab", "r");
			System.out.println ("Enter an integer, enter 'end' to stop");
			while (ctn) {
				line = s.nextLine ();
				try {
					int id = Integer.parseInt (line);
					pr.read (raf, id);
					System.out.println (pr);
				}
				catch (NumberFormatException e) {
					ctn = !line.trim ().equals ("end");
				}
			}
		}
		catch (FileNotFoundException ex) {
		}
		catch (IOException ex) {
			ex.printStackTrace ();
		}
		finally {
			if (player != null) {
				try {
					player.close ();
				}
				catch (IOException ex) {
				}
			}
		}

	}


}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
